import { createRouter, createWebHistory } from 'vue-router'
import Login from "@/views/Login.vue";
import Main from "@/views/Main.vue";
import ForgetPwd from "@/views/ForgetPwd.vue";


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:'/login',
      name:'login',
      component: Login
    },
    {
      path: '/forget',
      name: 'forget',
      component: ForgetPwd
    },
    {
      path:'/',
      redirect: '/login' //重定向
    },
    {
      path: '/main',
      name: 'main',
      component: Main,
      children:[
        {
          //首页
          path:'',
          name: 'Home',
          component: ()=>import('../components/Home.vue')
        },
        {
          //个人信息
          path: 'my',
          component: ()=>import('../components/My.vue')
        },
        {
          //修改密码组件
          path:'updatePwd',
          component:()=>import('../components/UpdatePwd.vue')
        },
        {
          //员工管理
          path: 'emp',
          component: ()=>import('../components/Emp.vue')
        }
      ]
    }

  ]
})
//全局守卫
router.beforeEach((to, from) => {
  //获取token
  let token = localStorage.getItem('token');
  if(to.path!=='/login' && !token){
    return {name: 'login'}
  }

  return true
})

export default router
